package com.bluej.mybatis.demo;

import java.io.Reader;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bluej.mybatis.model.Classes;
import com.bluej.mybatis.model.Student;
import com.bluej.mybatis.model.User;

public class Mybatis3 {
	
	public static Logger logger = LoggerFactory.getLogger(Mybatis3.class);
	private static SqlSessionFactory sqlSessionFactory;
    private static Reader reader;
    private static SqlSession session=null;

    static{
        try{
            reader    = Resources.getResourceAsReader("Configuration1.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
            session = sqlSessionFactory.openSession();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public static SqlSessionFactory getSession(){
        return sqlSessionFactory;
    }
    
    public static void main(String[] args) {
    	testGetClass3();
    	testGetClass3();
    	testGetClass3();
    	testGetClass3();
    	testGetClass3();
    	//testGetClass4();
    	//getStudent();
    	
    }
    public static void sleep(){
    	try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
        public static void testGetClass3(){
            /**
             * 映射sql的标识字符串，
             * me.gacl.mapping.classMapper是classMapper.xml文件中mapper标签的namespace属性的值，
             * getClass3是select标签的id属性值，通过select标签的id属性值就可以找到要执行的SQL
             */
            String statement = "com.bluej.mybatis.mapper.ClassMapper.getClass3";//映射sql的标识字符串
            //执行查询操作，将查询结果自动封装成Classes对象返回
            Classes clazz = session.selectOne(statement,1);//查询class表中id为1的记录
            //打印结果：Classes [id=1, name=class_a, teacher=Teacher [id=1, name=teacher1], students=[Student [id=1, name=student_A], Student [id=2, name=student_B], Student [id=3, name=student_C]]]
            System.out.println(clazz);
            logger.debug("--------------------------testGetClass3------------------------------");
            session.commit();	//测试二级缓存使用
            sleep();
        }
        
        public static void testGetClass4(){
            /**
             * 映射sql的标识字符串，
             * me.gacl.mapping.classMapper是classMapper.xml文件中mapper标签的namespace属性的值，
             * getClass4是select标签的id属性值，通过select标签的id属性值就可以找到要执行的SQL
             */
            String statement = "com.bluej.mybatis.mapper.ClassMapper.getClass4";//映射sql的标识字符串
            //执行查询操作，将查询结果自动封装成Classes对象返回
            Classes clazz = session.selectOne(statement,1);//查询class表中id为1的记录
            
            //打印结果：Classes [id=1, name=class_a, teacher=Teacher [id=1, name=teacher1], students=[Student [id=1, name=student_A], Student [id=2, name=student_B], Student [id=3, name=student_C]]]
            System.out.println(clazz);
            logger.debug("--------------------------testGetClass4------------------------------");
            session.commit();	//测试二级缓存使用
            sleep();
        }
        
        public  static void getStudent(){
        	String statement = "com.bluej.mybatis.mapper.ClassMapper.getStudent";//映射sql的标识字符串
        	List<Student> students = session.selectList(statement, 1);//查询class表中id为1的记录
        	System.out.println(students);
        }
        
        
        public  static void close(){
        	//使用SqlSession执行完SQL之后需要关闭SqlSession
            session.close();
        }
}
